home *** CD-ROM | disk | FTP | other *** search
/ CD/PC Actual 3 / CD ACTUAL 3.iso / linux / incoming / libgr-2.000 / libgr-2 / libgr-2.0.3 / tiff / tif_tile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-20  |  5.9 KB  |  214 lines

  1. /* /usr/local/src/CVS/libgr/tiff/tif_tile.c,v 1.2 1995/08/20 12:51:30 neal Exp */
  2.  
  3. /*
  4.  * Copyright (c) 1991-1995 Sam Leffler
  5.  * Copyright (c) 1991-1995 Silicon Graphics, Inc.
  6.  *
  7.  * Permission to use, copy, modify, distribute, and sell this software and 
  8.  * its documentation for any purpose is hereby granted without fee, provided
  9.  * that (i) the above copyright notices and this permission notice appear in
  10.  * all copies of the software and related documentation, and (ii) the names of
  11.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  12.  * publicity relating to the software without the specific, prior written
  13.  * permission of Sam Leffler and Silicon Graphics.
  14.  * 
  15.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  16.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  17.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  18.  * 
  19.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  20.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  21.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  23.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  24.  * OF THIS SOFTWARE.
  25.  */
  26.  
  27. /*
  28.  * TIFF Library.
  29.  *
  30.  * Tiled Image Support Routines.
  31.  */
  32. #include "tiffiop.h"
  33.  
  34. /*
  35.  * Compute which tile an (x,y,z,s) value is in.
  36.  */
  37. ttile_t
  38. TIFFComputeTile(TIFF* tif, uint32 x, uint32 y, uint32 z, tsample_t s)
  39. {
  40.     TIFFDirectory *td = &tif->tif_dir;
  41.     uint32 dx = td->td_tilewidth;
  42.     uint32 dy = td->td_tilelength;
  43.     uint32 dz = td->td_tiledepth;
  44.     ttile_t tile = 1;
  45.  
  46.     if (td->td_imagedepth == 1)
  47.         z = 0;
  48.     if (dx == (uint32) -1)
  49.         dx = td->td_imagewidth;
  50.     if (dy == (uint32) -1)
  51.         dy = td->td_imagelength;
  52.     if (dz == (uint32) -1)
  53.         dz = td->td_imagedepth;
  54.     if (dx != 0 && dy != 0 && dz != 0) {
  55.         uint32 xpt = TIFFhowmany(td->td_imagewidth, dx); 
  56.         uint32 ypt = TIFFhowmany(td->td_imagelength, dy); 
  57.         uint32 zpt = TIFFhowmany(td->td_imagedepth, dz); 
  58.  
  59.         if (td->td_planarconfig == PLANARCONFIG_SEPARATE) 
  60.             tile = (xpt*ypt*zpt)*s +
  61.                  (xpt*ypt)*(z/dz) +
  62.                  xpt*(y/dy) +
  63.                  x/dx;
  64.         else
  65.             tile = (xpt*ypt)*(z/dz) + xpt*(y/dy) + x/dx + s;
  66.     }
  67.     return (tile);
  68. }
  69.  
  70. /*
  71.  * Check an (x,y,z,s) coordinate
  72.  * against the image bounds.
  73.  */
  74. TIFFCheckTile(TIFF* tif, uint32 x, uint32 y, uint32 z, tsample_t s)
  75. {
  76.     TIFFDirectory *td = &tif->tif_dir;
  77.  
  78.     if (x >= td->td_imagewidth) {
  79.         TIFFError(tif->tif_name, "Col %ld out of range, max %lu",
  80.             (long) x, (u_long) td->td_imagewidth);
  81.         return (0);
  82.     }
  83.     if (y >= td->td_imagelength) {
  84.         TIFFError(tif->tif_name, "Row %ld out of range, max %lu",
  85.             (long) y, (u_long) td->td_imagelength);
  86.         return (0);
  87.     }
  88.     if (z >= td->td_imagedepth) {
  89.         TIFFError(tif->tif_name, "Depth %ld out of range, max %lu",
  90.             (long) z, (u_long) td->td_imagedepth);
  91.         return (0);
  92.     }
  93.     if (td->td_planarconfig == PLANARCONFIG_SEPARATE &&
  94.         s >= td->td_samplesperpixel) {
  95.         TIFFError(tif->tif_name, "Sample %d out of range, max %u",
  96.             (int) s, td->td_samplesperpixel);
  97.         return (0);
  98.     }
  99.     return (1);
  100. }
  101.  
  102. /*
  103.  * Compute how many tiles are in an image.
  104.  */
  105. ttile_t
  106. TIFFNumberOfTiles(TIFF* tif)
  107. {
  108.     TIFFDirectory *td = &tif->tif_dir;
  109.     uint32 dx = td->td_tilewidth;
  110.     uint32 dy = td->td_tilelength;
  111.     uint32 dz = td->td_tiledepth;
  112.     ttile_t ntiles;
  113.  
  114.     if (dx == (uint32) -1)
  115.         dx = td->td_imagewidth;
  116.     if (dy == (uint32) -1)
  117.         dy = td->td_imagelength;
  118.     if (dz == (uint32) -1)
  119.         dz = td->td_imagedepth;
  120.     ntiles = (dx == 0 || dy == 0 || dz == 0) ? 0 :
  121.         (TIFFhowmany(td->td_imagewidth, dx) *
  122.          TIFFhowmany(td->td_imagelength, dy) *
  123.          TIFFhowmany(td->td_imagedepth, dz));
  124.     if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
  125.         ntiles *= td->td_samplesperpixel;
  126.     return (ntiles);
  127. }
  128.  
  129. /*
  130.  * Compute the # bytes in each row of a tile.
  131.  */
  132. tsize_t
  133. TIFFTileRowSize(TIFF* tif)
  134. {
  135.     TIFFDirectory *td = &tif->tif_dir;
  136.     tsize_t rowsize;
  137.     
  138.     if (td->td_tilelength == 0 || td->td_tilewidth == 0)
  139.         return ((tsize_t) 0);
  140.     rowsize = td->td_bitspersample * td->td_tilewidth;
  141.     if (td->td_planarconfig == PLANARCONFIG_CONTIG)
  142.         rowsize *= td->td_samplesperpixel;
  143.     return ((tsize_t) TIFFhowmany(rowsize, 8));
  144. }
  145.  
  146. /*
  147.  * Compute the # bytes in a variable length, row-aligned tile.
  148.  */
  149. tsize_t
  150. TIFFVTileSize(TIFF* tif, uint32 nrows)
  151. {
  152.     TIFFDirectory *td = &tif->tif_dir;
  153.     tsize_t tilesize;
  154.  
  155.     if (td->td_tilelength == 0 || td->td_tilewidth == 0 ||
  156.         td->td_tiledepth == 0)
  157.         return ((tsize_t) 0);
  158. #ifdef YCBCR_SUPPORT
  159.     if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
  160.         td->td_photometric == PHOTOMETRIC_YCBCR &&
  161.         !isUpSampled(tif)) {
  162.         /*
  163.          * Packed YCbCr data contain one Cb+Cr for every
  164.          * HorizontalSampling*VerticalSampling Y values.
  165.          * Must also roundup width and height when calculating
  166.          * since images that are not a multiple of the
  167.          * horizontal/vertical subsampling area include
  168.          * YCbCr data for the extended image.
  169.          */
  170.         tsize_t w =
  171.             TIFFroundup(td->td_tilewidth, td->td_ycbcrsubsampling[0]);
  172.         tsize_t rowsize = TIFFhowmany(w*td->td_bitspersample, 8);
  173.         tsize_t samplingarea =
  174.             td->td_ycbcrsubsampling[0]*td->td_ycbcrsubsampling[1];
  175.         nrows = TIFFroundup(nrows, td->td_ycbcrsubsampling[1]);
  176.         /* NB: don't need TIFFhowmany here 'cuz everything is rounded */
  177.         tilesize = nrows*rowsize + 2*(nrows*rowsize / samplingarea);
  178.     } else
  179. #endif
  180.         tilesize = nrows * TIFFTileRowSize(tif);
  181.     return ((tsize_t)(tilesize * td->td_tiledepth));
  182. }
  183.  
  184. /*
  185.  * Compute the # bytes in a row-aligned tile.
  186.  */
  187. tsize_t
  188. TIFFTileSize(TIFF* tif)
  189. {
  190.     return (TIFFVTileSize(tif, tif->tif_dir.td_tilelength));
  191. }
  192.  
  193. /*
  194.  * Compute a default tile size based on the image
  195.  * characteristics and a requested value.  If a
  196.  * request is <1 then we choose a size according
  197.  * to certain heuristics.
  198.  */
  199. void
  200. TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
  201. {
  202.     (*tif->tif_deftilesize)(tif, tw, th);
  203. }
  204.  
  205. void
  206. _TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
  207. {
  208.     (void) tif;
  209.     if (*(int32*) tw < 1)
  210.         *tw = 256;
  211.     if (*(int32*) th < 1)
  212.         *th = 256;
  213. }
  214.